const initState = {
	city: [],
	list: [],
	car: []
}
// actionType类型
export const SET_CITY = 'SET_CITY'
export const SET_LIST = 'SET_LIST'
export const ADD_CAR_ITEM = 'ADD_CAR_ITEM'
export const SUB_CAR_ITEM = 'SUB_CAR-ITEM'

const reducer = (state = initState, action) => {
	const { type, payload } = action
	const { car } = state
	switch (type) {
		// 设置城市数据
		case SET_CITY:
			return {
				...state,
				city: payload
			};
		// 设置首页列表数据
		case SET_LIST:
			return {
				...state,
				list: payload
			};
		// 添加购物车
		case ADD_CAR_ITEM:
			// 找数据  通过id
			const result = car.find(item => item.id === payload.id)
			if (result) {
				// 存在 找到了
				result.count++
			} else {
				// 没有
				car.push({
					...payload,
					count: 1,
					isChcked: false
				})
			}
			console.log('购物车数据添加了', car)
			return {
				...state,
				car: [...car]
			};
		// 减少购物车
		case SUB_CAR_ITEM:
			// 找数据  通过id
			const result1 = car.find(item => item.id === payload.id)

			result1.count--
			return {
				...state,
				car: [...car]
			}
		default:
			return state
	}
}


export default reducer